package com.blog.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Bean
    public Queue esQueue(){
        return new Queue("ESQueue",true);
    }
    @Bean
    public Exchange esExchange(){
        return new DirectExchange("ESExchange",true,false);
    }
    @Bean
    public Binding bind(){

        return BindingBuilder.bind(esQueue()).to(esExchange()).with("es").noargs();

    }
    @Bean
    public RabbitTemplate rabbitTemplate(){
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(((correlationData, ack, cause) -> {
                    System.out.println(correlationData.getId() + " ack:" + ack + " cause:" + cause);
                })

        );
        rabbitTemplate.setReturnCallback(((message, replyCode, replyText, exchange, routingKey) -> {
            System.out.println("return:"+message.getMessageProperties().getMessageId());
        }));
        return rabbitTemplate;
    }
}
